package com.jgzx.security.feign;


import cn.hutool.core.util.ObjectUtil;
import com.jgzx.constant.Constants;
import com.jgzx.utils.WebUtil;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * feign 请求拦截器
 */
@Slf4j
@Configuration
public class FeignConfiguration implements RequestInterceptor {

    @Override
    public void apply(RequestTemplate requestTemplate) {
        HttpServletRequest request = WebUtil.getRequest();
        if (ObjectUtil.isNotNull(request)) {
            Map<String, String> header = WebUtil.getHeaderParams(request);
            // 传递用户信息请求头，防止丢失
            requestTemplate.header(Constants.HEADER_TOKEN, header.get(Constants.HEADER_TOKEN))
                    .header(Constants.HEADER_USER_ID, header.get(Constants.HEADER_USER_ID))
                    .header(Constants.HEADER_PHONE_NUM, header.get(Constants.HEADER_PHONE_NUM))
                    .header(Constants.HEADER_ENTERPRISE_ID, header.get(Constants.HEADER_ENTERPRISE_ID))
                    .header(Constants.HEADER_APP_KEY, header.get(Constants.HEADER_APP_KEY))
                    .header(Constants.HEADER_APP_VERSION, header.get(Constants.HEADER_APP_VERSION))
                    .header(Constants.HEADER_CLIENT_TYPE, header.get(Constants.HEADER_CLIENT_TYPE))
                    .header(Constants.HEADER_APP_CHANNEL, header.get(Constants.HEADER_APP_CHANNEL))
                    .header(Constants.HEADER_DEVICE_ID, header.get(Constants.HEADER_DEVICE_ID))
                    .header(Constants.HEADER_DEVICE_BRAND, header.get(Constants.HEADER_DEVICE_BRAND))
                    .header(Constants.HEADER_DEVICE_MODEL, header.get(Constants.HEADER_DEVICE_MODEL))
                    .header(Constants.HEADER_DEVICE_OS, header.get(Constants.HEADER_DEVICE_OS));
        }
    }

}